import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/city_information.dart';
import 'package:yp/services/api.dart';

import '../../helper.dart';
import 'state.dart';

class AddressCreateLogic extends GetxController {
  final state = AddressCreateState();

  @override
  void onInit() {
    super.onInit();

    var cityParams = {
      "access_token": SpUtil.getString('token'),
    };
    Api.cityInformation(cityParams).listen((event) {
      // print('城市信息  event = $event');
      if (event['code'] == 0) {
        AddressInformation addressInformation =
            AddressInformation.fromJson(event);
        Map<String, dynamic> mapData = addressInformation.toJson();
        state.addressList = mapData['children'];
      }
      update();
    });

    String gasTag = SpUtil.getString("addressTag", defValue: "");

    if (gasTag.isNotEmpty) {
      state.tagList[2] = gasTag;
    }
  }

  void setSex(int sex) {
    state.sex = sex;
    update();
  }

  void addAddress() {
    var userNumId = Get.arguments != null && Get.arguments['userNum'] != null
        ? Get.arguments['userNum']
        : SpUtil.getInt('empeNumId');

    var params = {
      'usr_num_id': userNumId,
      "channel_num_id": 99,
      "default_sign": state.isSelect ? 1 : 0,
      "prv_num_id": state.provinceId,
      'city_num_id': state.cityId,
      'city_area_num_id': state.areaId,
      'town_num_id': state.townId,
      'adr': state.controller4.text,
      'mobile': state.controller2.text,
      'receiver': state.controller1.text,
      'address_tag': state.tagList[state.sex - 1],
    };
  
    //获取选择商品列表数据
    Api.addAddress(params).listen((event) {
      print('=========-------=========---------  event = $event');
      if (event['code'] == 0) {
        toast("添加成功");
        Get.back(result: true);
      } else {
        print('添加失败！！！  event = $event');
      }
      update();
    });
  }

  /// 选择地址
  // void selectCity(value) {
  //   state.provinceName = value.province;
  //   state.cityName = value.city;
  //   state.areaName = value.area;
  //   state.townName = value.town;
  //   state.provinceId = int.parse(value.provinceCode);
  //   state.cityId = int.parse(value.cityCode);
  //   state.areaId = int.parse(value.areaCode);
  //   state.townId = int.parse(value.townCode);
  //   state.controller3.text =
  //       value.province + ' ' + value.city + ' ' + value.area;
  //   update();
  // }
  void selectCity(int index, Map checkedItem, List backAddressItemList) {
    switch (index) {
      case 0:
        state.province = checkedItem;
        state.provinceList = backAddressItemList;

        state.city = {};
        state.cityList = [];
        state.district = {};
        state.cityAreaList = [];
        state.street = {};
        state.townList = [];

        break;
      case 1:
        state.city = checkedItem;
        state.cityList = backAddressItemList;

        state.district = {};
        state.cityAreaList = [];
        state.street = {};
        state.townList = [];

        break;
      case 2:
        state.district = checkedItem;
        state.cityAreaList = backAddressItemList;

        state.street = {};
        state.townList = [];

        break;
      case 3:
        state.street = checkedItem;
        state.townList = backAddressItemList;
        break;
    }

    if (index == 3) {
      state.provinceName = state.province["name"];
      state.cityName = state.city["name"];
      state.areaName = state.district["name"];
      state.townName = state.street["name"];
      state.provinceId = state.province["id"];
      state.cityId = state.city["id"];
      state.areaId = state.district["id"];
      state.townId = state.street["id"];

      state.controller3.text = state.provinceName +' '+ state.cityName+' '+ state.areaName + (state.townName == null ? "" : (" " + state.townName));
      update();
    }
  }
}
